package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysSemester;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysSemesterService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学期Controller
 *
 * @author zjj
 * @date 2020-06-27
 */
@Controller
@RequestMapping("/system/semester")
public class SysSemesterController extends BaseController
{
    private String prefix = "system/semester";

    @Autowired
    private ISysSemesterService sysSemesterService;

    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("system:semester:view")
    @GetMapping()
    public String semester(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/semester";
    }

    /**
     * 查询学期列表
     */
    @RequiresPermissions("system:semester:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSemester sysSemester)
    {
        startPage();
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        List<SysSemester> list = sysSemesterService.selectSysSemesterList(sysSemester);
        return getDataTable(list);
    }

    /**
     * 导出学期列表
     */
    @RequiresPermissions("system:semester:export")
    @Log(title = "学期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSemester sysSemester)
    {
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        List<SysSemester> list = sysSemesterService.selectSysSemesterList(sysSemester);
        ExcelUtil<SysSemester> util = new ExcelUtil<SysSemester>(SysSemester.class);
        return util.exportExcel(list, "semester");
    }

    /**
     * 新增学期
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存学期
     */
    @RequiresPermissions("system:semester:add")
    @Log(title = "学期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSemester sysSemester)
    {
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysSemesterService.checkNameUnique(sysSemester))) {
            return error("新增学期'" + sysSemester.getName() + "'失败，名称已存在");
        }
        sysSemester.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysSemesterService.insertSysSemester(sysSemester));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysSemester sysSemester) {
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        return sysSemesterService.checkNameUnique(sysSemester);
    }

    /**
     * 修改学期
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysSemester", sysSemesterService.selectSysSemesterById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存学期
     */
    @RequiresPermissions("system:semester:edit")
    @Log(title = "学期", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSemester sysSemester)
    {
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysSemesterService.checkNameUnique(sysSemester))) {
            return error("修改学期'" + sysSemester.getName() + "'失败，名称已存在");
        }
        sysSemester.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysSemesterService.updateSysSemester(sysSemester));
    }

    /**
     * 删除学期
     */
    @RequiresPermissions("system:semester:remove")
    @Log(title = "学期", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysSemester semester = new SysSemester();
        semester.setIds(Convert.toStrArray(ids));
        semester.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            semester.setTenantId(user.getTenantId());
        }
        return toAjax(sysSemesterService.deleteSysSemesterByIds(semester));
    }

    /**
     * 状态修改
     */
    @Log(title = "学期管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:semester:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysSemester sysSemester)
    {
        sysSemester.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysSemesterService.updateSysSemester(sysSemester));
    }

}
